package com.whq.mvc.base.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class RedissonUtils {

    final static Logger logger = LoggerFactory.getLogger(RedissonUtils.class);

    private static RedissonUtils redissonUtils;

    private RedissonUtils() {
    }

    public static RedissonUtils getInstance() {
        if (Objects.isNull(redissonUtils)) {
            synchronized (RedissonUtils.class) {
                if (Objects.isNull(redissonUtils)) {
                    redissonUtils = new RedissonUtils();
                }
            }
        }
        return redissonUtils;
    }

    public RedissonClient getRedisson(String ip, String port) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + ip + ":" + port).setPassword("123456");
        RedissonClient client = Redisson.create(config);
        logger.info("redisson成功连接redis server");
        return client;
    }

    public void closeRedisson(RedissonClient client) {
        client.shutdown();
        logger.info("成功关闭redisson client");
    }
}
